<template>
  <div>
    <van-picker show-toolbar :default-index='defaultIndex' :columns="columns" @confirm="onConfirm" @cancel="$emit('close')" @change="onChange" />
  </div>
</template>
<script>
import { updateUserProfile } from '@/api/user.js'
export default {
  props: {
    value: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      columns: ['男', '女'],
      defaultIndex: this.value
    }
  },
  methods: {
    onChange(picker, value, index) {
      this.defaultIndex = index
    },
    async onConfirm() {
      this.$toast.loading({
        message: '保存中...',
        forbidClick: true
      })
      await updateUserProfile({
        gender: this.defaultIndex
      })
      this.$emit('input', this.defaultIndex)
      this.$emit('close')
      this.$toast.success('保存成功！')
    }
  }
}
</script>
